# -*- coding: utf-8 -*-
from datetime import datetime

import pandas as pd
from docxtpl import DocxTemplate


def main(StuNo):
    path = '数据/计通20学生花名册.xlsx'
    data = pd.read_excel(path)  # 原始信息

    filtered_data = data[data['学号'].astype(str) == StuNo]
    if not filtered_data.empty:
        filtered_data = filtered_data.iloc[0]
    else:
        print('没有找到匹配的学号')
    # 插入信息
    name = filtered_data['姓名']
    sex = filtered_data['性别']
    DOB = pd.to_datetime(filtered_data['出生日期'], format='%Y/%m/%d').strftime('%Y年%m月%d日')
    ID = filtered_data['身份证号']
    entranceYear = filtered_data['入学年份']
    grade = filtered_data['当前所在年级']
    major = filtered_data['专业']
    cls = filtered_data['班级']
    date = datetime.now().date().strftime("%Y年%m月%d日")

    tpl = DocxTemplate('模板/在校生学籍证明模板.docx')
    context = {
        'name': name,
        'sex': sex,
        'DOB': DOB,
        'ID': ID,
        'entranceYear': entranceYear,
        'grade': grade,
        'major': major,
        'cls': cls,
        'StuNo': StuNo,
        'date': date
    }
    tpl.render(context)
    word_file = f'{StuNo}-{name}.docx'
    tpl.save(word_file)
    print('文件已生成!')


if __name__ == '__main__':
    main(input('请输入学号: '))
